#include <string.h>
#include "at.h"
#include "rtthread.h"
#include "arpa/inet.h"
#include "netdev.h"
#include <sys/socket.h>
#include <netdb.h>
#include "at_device.h"

#define DBG_TAG "n32_esp8266.c"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

/**
 * @brief  获取esp8266的链接状态
 * @return 无
 * @return 成功返回0，失败返回-1
 */
int get_esp8266_link_status(void)
{
    /* "esp8266" 名字来源于设备注册时使用的名字，在文件 packages/at_device-latest/samples/at_sample_esp8266.c 中定义 */
    struct at_device * esp8266_dev = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, "esp0");
    if (esp8266_dev == RT_NULL)
    {
        LOG_E("func: %s. get esp8266 at device failed", __FUNCTION__);
        return -1;
    }

    if (esp8266_dev->is_init == RT_FALSE)
    {
        LOG_E("%s is not init", "esp0");
        return -1;
    }

    if ((esp8266_dev->netdev->flags & NETDEV_FLAG_UP) == 0)
    {
        return -1;
    }

    if ((esp8266_dev->netdev->flags & NETDEV_FLAG_INTERNET_UP) == NETDEV_FLAG_INTERNET_UP)
    {
        return 0;
    }
    else
    {
        return -1;
    }
}
